package archive

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/archive"
	archiveReq "github.com/flipped-aurora/gin-vue-admin/server/model/archive/request"
	"gorm.io/gorm"
)

type ArchiveTypeService struct {
}

// CreateArchiveType 创建档案类型记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveTypeService *ArchiveTypeService) CreateArchiveType(archiveType *archive.ArchiveType) (err error) {
	err = global.GVA_DB.Create(archiveType).Error
	return err
}

// DeleteArchiveType 删除档案类型记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveTypeService *ArchiveTypeService) DeleteArchiveType(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&archive.ArchiveType{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&archive.ArchiveType{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteArchiveTypeByIds 批量删除档案类型记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveTypeService *ArchiveTypeService) DeleteArchiveTypeByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&archive.ArchiveType{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&archive.ArchiveType{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateArchiveType 更新档案类型记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveTypeService *ArchiveTypeService) UpdateArchiveType(archiveType archive.ArchiveType) (err error) {
	err = global.GVA_DB.Model(&archive.ArchiveType{}).Where("id = ?", archiveType.ID).Updates(&archiveType).Error
	return err
}

// GetArchiveType 根据ID获取档案类型记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveTypeService *ArchiveTypeService) GetArchiveType(ID string) (archiveType archive.ArchiveType, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&archiveType).Error
	return
}

// GetArchiveTypeInfoList 分页获取档案类型记录
// Author [piexlmax](https://github.com/piexlmax)
func (archiveTypeService *ArchiveTypeService) GetArchiveTypeInfoList(info archiveReq.ArchiveTypeSearch) (list []archive.ArchiveType, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&archive.ArchiveType{})
	var archiveTypes []archive.ArchiveType
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&archiveTypes).Error
	return archiveTypes, total, err
}

// GetArchiveTypeByName 根据ID获取档案类型记录
func (archiveTypeService *ArchiveTypeService) GetArchiveTypeByName(name string) (archiveType archive.ArchiveType, err error) {
	err = global.GVA_DB.Where("name = ?", name).First(&archiveType).Error
	return
}
